package com.xiaohudie.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaohudie.dao.Users;
import com.xiaohudie.mapper.UsersMapper;
import org.springframework.security.authentication.jaas.AuthorityGranter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userDetailsService")
public class MyUserDetailService implements UserDetailsService {
    @Resource
    private UsersMapper usersMapper;
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      /**根据条件查询用户名是否存在*/
        QueryWrapper<Users> queryWrapper =new QueryWrapper<Users>();
        queryWrapper.eq("user_name",username);
        Users users = usersMapper.selectOne(queryWrapper);
        if (users==null){
            throw  new UsernameNotFoundException("用户名不存在！！！！！");
        }
        /**admins 后期是从数据里查出来进行验证得来的权限*/
        List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList("admins");
        return new User(users.getUserName(),new BCryptPasswordEncoder().encode(users.getPassWord()),list);
    }

}
